package com.seventeen.mq.proxy.server.proxy.ext;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.client.apis.message.MessageView;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

/**
 * 代理消息实体类
 *
 * @author seventeen
 */
public class ExtProxyMessage
{

    private String keys;
    private String msgId;
    private String message;
    private String topic;
    private String tags;
    private int reconsumeTimes;

    public static String convert(MessageExt messageExt)
    {
        ExtProxyMessage extProxyMessage = new ExtProxyMessage();
        extProxyMessage.setTags(messageExt.getTags());
        extProxyMessage.setReconsumeTimes(messageExt.getReconsumeTimes());
        extProxyMessage.setTopic(messageExt.getTopic());
        extProxyMessage.setKeys(messageExt.getKeys());
        extProxyMessage.setMsgId(messageExt.getMsgId());
        extProxyMessage.setMessage(new String(messageExt.getBody()));
        return JSON.toJSONString(extProxyMessage);
    }

    public static String convert(MessageView messageView)
    {
        ByteBuffer byteBuffer = messageView.getBody();
        String message = StandardCharsets.UTF_8.decode(byteBuffer).toString();
        byteBuffer.flip();
        ExtProxyMessage extProxyMessage = new ExtProxyMessage();
        extProxyMessage.setTags(messageView.getTag().orElse("*"));
        extProxyMessage.setTopic(messageView.getTopic());
        extProxyMessage.setKeys(messageView.getKeys().toString());
        extProxyMessage.setMsgId(String.valueOf(messageView.getMessageId()));
        extProxyMessage.setMessage(message);
        return JSON.toJSONString(extProxyMessage);
    }

    public String getKeys() { return keys; }

    public void setKeys(String keys) { this.keys = keys; }

    public String getMsgId() { return msgId; }

    public void setMsgId(String msgId) { this.msgId = msgId; }

    public String getMessage() { return message; }

    public void setMessage(String message) { this.message = message; }

    public String getTopic() { return topic; }

    public void setTopic(String topic) { this.topic = topic; }

    public String getTags() { return tags; }

    public void setTags(String tags) { this.tags = tags; }

    public int getReconsumeTimes() { return reconsumeTimes; }

    public void setReconsumeTimes(int reconsumeTimes) { this.reconsumeTimes = reconsumeTimes; }
}
